package com.ug_project.adapters

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.ug_project.objects.isNotNull
import com.ug_project.objects.isNull
import com.ug_project.objects.toArrayList

/**
 * Created by yeganaaa on 3/7/2017.
 */
open class RecyclerAdapter <T> (val context: Context, var defaultlayout_resID: Int,var data: ArrayList<T>) : RecyclerView.Adapter<RecyclerViewHolder>() {
    private var filterData=ArrayList<T>()
    private var _OnItemCount : (() -> Int)? = null
    private var _GetItemViewType: ((position: Int) -> Int) = {defaultlayout_resID}
    private var _OnBindViewHolder: ((itemview: View, itemViewResID: Int, data: ArrayList<T>, position: Int) -> Unit)? = null
    private var _OnBindViewHolderForItemData: ((itemview: View, itemViewResID: Int, data: T, position: Int) -> Unit)? = null
    var ItemViewResIDs = HashMap<Int, Int>()
    private  var _inflater: LayoutInflater? = null
    private var filterAction: (T) -> Boolean = {true}

    open fun OnItemCount(getcount: () -> Int)
    {
        _OnItemCount = getcount
    }

    open fun GetItemViewType(getviewtype: (position: Int) -> Int)
    {
        _GetItemViewType = getviewtype
    }

    open fun OnBindViewHolder(binder: (rootView: View, itemViewResID: Int, data: ArrayList<T>, position: Int) -> Unit)
    {
        _OnBindViewHolder = binder
    }

    /**
     * On Bind View Holder For Item Data
     */
    open fun OnBindViewHolderIData(binder: (rootView: View, itemViewResID: Int, data: T, position: Int) -> Unit)
    {
        _OnBindViewHolderForItemData = binder
    }

    override fun onBindViewHolder(holder: RecyclerViewHolder?, position: Int) {
//        _OnBindViewHolder.isNull {
//            throw Exception("OnBindViewHolder ئەمەلگە ئاشۇرۇلىشى كىرەك")
//        }
        _OnBindViewHolder?.invoke(holder!!.ItemView, ItemViewResIDs[position]!!, data, position)
        _OnBindViewHolderForItemData?.invoke(holder!!.ItemView, ItemViewResIDs[position]!!, data[position], position)
    }

    init {
        filterData.clear()
        filterData.addAll(data)
    }

    fun filter(action: (T) -> Boolean)
    {
        filterAction = action
        filterData.clear()
        filterData.addAll(data.filter { filterAction(it) })
        notifyDataSetChanged()
    }

    open fun refresh(){
        filter(filterAction)
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerViewHolder {
        _inflater.isNull {
            _inflater = LayoutInflater.from(context)
        }
        //Get Item View Resource ID
        val resid = _GetItemViewType(viewType)
        //Add Item View Resource ID To Map
        ItemViewResIDs.put(viewType, resid)

        val _view = _inflater!!.inflate(resid, parent, false)
        return RecyclerViewHolder(_view)
    }

    override fun getItemCount(): Int {
        var count = 0

//        data.isNull {
//            _OnItemCount.isNull {
//                throw Exception("GetItemCount چوقۇم يىزىلغان بولىشى كىرەك")
//            }
//        }

        _OnItemCount.isNotNull {
            count = it()
        }

        _OnItemCount.isNull {
            count = filterData.size
        }

//        data.isNotNull {
            count = filterData.size
//        }
        return  count
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

}

fun <T> createRecyclerAdapter(context: Context, defaultlayout_resID: Int, data: ArrayList<T>, creator: RecyclerAdapter<T>.() -> Unit) : RecyclerAdapter<T>
{
    var adapter = RecyclerAdapter<T>(context, defaultlayout_resID, data)
    adapter.creator()
    return adapter
}
open class RecyclerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
{
    var ItemView = itemView
}